using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace OA.BLL
{
    public class DialogField:Base
    {
        private DAL.DialogField dal = null;

        public DialogField()
        {
            isession = DB_help.NHibernateHelper.GetCurrentSession(DefaultDataBase);
            dal=new DAL.DialogField(isession);
        }

        public Common.Result Add(Model.DialogField value,bool? IsResult)
        {
            Common.Result ret = new Common.Result(false, 0, "添加失败");
            value.Dialog = new DAL.Dialog(isession).GetModel(value.DialogCode);
            if (value.Dialog == null)
            {
                ret.message = "[对话框编号]不存在";
                return ret;
            }
            if (dal.Get(value.DialogCode,value.FieldKey) != null)
            {
                ret.message = "[编号]已经存在";
                return ret;
            }
            var tran=isession.BeginTransaction();
            ret = dal.Add(value);
            if (ret.success)
            {
                value.Dialog.ResultFieldKey = value.FieldKey;
            }
            tran.Commit();
            return ret;
        }

        public Common.Result Update(Model.DialogField value,bool? IsResult)
        {
            Common.Result ret = new Common.Result(false, 0, "修改失败");
            var dalvalue = dal.GetModel(value.ID);
            if (dalvalue == null)
            {
                ret.success = false;
                ret.count = 0;
                ret.message = "数据不存在";
                return ret;
            }
            dalvalue.Title = value.Title;
            dalvalue.FieldKey = value.FieldKey;
            dalvalue.IsShow = value.IsShow;
            dalvalue.Sort = value.Sort;
            dalvalue.Width = value.Width;
            if (IsResult==true) dalvalue.Dialog.ResultFieldKey = value.FieldKey;
            ret = dal.Update(dalvalue);
            return ret;
        }

        public Common.Result Delete(string ids)
        {
            var ret=dal.Delete(ids);
            return ret;
        }

        public Model.DialogField Get(int id)
        {
            return dal.GetModel(id);
        }

        public IList<Model.DialogField> Get(string where, string orderby, int start, int length, params object[] ps)
        {
            return dal.Get(where, orderby, start, length, ps);
        }

        public int GetCount(string where, params object[] ps)
        {
            return dal.GetCount(where, ps);
        }
    }
}
